package dao.impl;

import dao.BaseDao;
import dao.DistributionDao;
import entity.Distribution;
import entity.Userinfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DistributionDaoImpl extends BaseDao implements DistributionDao {

    /**
     * 查询方法
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Distribution> selectAll(int pageNo, int pageSize) {
        List<Distribution> list=new ArrayList<>();
        Distribution d=null;
        String sql="SELECT * FROM `yipinpindb`.`distribution` LIMIT ?,?";
        ResultSet rs=null;
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            rs=exeQuery(sql, new Object[]{
                    (pageNo-1)*pageSize,
                    pageSize
            },conn,ps);
            while (rs.next()){
                d=new Distribution();
                d.setId(rs.getInt(1));
                d.setdName(rs.getString(2));
                d.setdDescribe(rs.getString(3));
                d.setdPath(rs.getString(4));
                list.add(d);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }finally {
            closeAll(rs,ps,conn);
        }
        return list;
    }

    /**
     * 新增方法
     * @param d
     * @return
     */
    @Override
    public int insert(Distribution d) {
        String sql="INSERT INTO `yipinpindb`.`distribution`(`d_name`, `d_describe`, `d_path`) VALUES (?, ?, ?)";
        return exeUpdate(sql,new Object[]{
                d.getdName(),
                d.getdDescribe(),
                d.getdPath()
        });
    }

    /**
     * 修改方法
     * @param d
     * @return
     */
    @Override
    public int update(Distribution d) {
        String sql="UPDATE `yipinpindb`.`distribution` SET `d_name` = ?, `d_describe` = ?, `d_path` = ?  WHERE `id` = ?";
        return exeUpdate(sql,new Object[]{
                d.getdName(),
                d.getdDescribe(),
                d.getdPath(),
                d.getId()
        });
    }

    /**
     * 查询总记录数
     * @return
     */
    @Override
    public int selectCount() {
        String sql="select count(0) from distribution";
        ResultSet rs=null;
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            rs=exeQuery(sql, null,conn,ps);
            while (rs.next()){
                return  rs.getInt(1);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }finally {
            closeAll(rs,ps,conn);
        }
        return 0;
    }

    /**
     * 根据id查询单个数据
     * @param id
     * @return
     */
    @Override
    public Distribution selectOne(int id) {
        Distribution d=null;
        String sql="select * from distribution where id=?";
        ResultSet rs=null;
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            rs=exeQuery(sql, new Object[]{
                    id
            },conn,ps);
            while (rs.next()){
                d=new Distribution();
                d.setId(rs.getInt(1));
                d.setdName(rs.getString(2));
                d.setdDescribe(rs.getString(3));
                d.setdPath(rs.getString(4));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }finally {
            closeAll(rs,ps,conn);
        }
        return d;
    }

    /**
     * 删除方法
     *
     * @param id
     * @return
     */
    @Override
    public int delete(int id) {
        String sql="delete from distribution where id=?";
        return exeUpdate(sql,new Object[]{
                 id
        });
    }
}
